package org.jeecg.modules.demo.hjyzbj.controller;

import org.jeecg.common.system.query.QueryGenerator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import java.util.Arrays;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.hjyzbj.entity.ZwglEltqsz;
import org.jeecg.modules.demo.hjyzbj.entity.ZwglHjyzsz;
import org.jeecg.modules.demo.hjyzbj.entity.ZwglEltq;
import org.jeecg.modules.demo.hjyzbj.service.IZwglEltqService;
import org.jeecg.modules.demo.hjyzbj.service.IZwglEltqszService;
import org.jeecg.modules.demo.hjyzbj.service.IZwglHjyzszService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 恶劣天气
 * @Author: jeecg-boot
 * @Date:   2023-04-19
 * @Version: V1.0
 */
@Api(tags="恶劣天气")
@RestController
@RequestMapping("/hjyzbj/zwglEltq")
@Slf4j
public class ZwglEltqController extends JeecgController<ZwglEltq, IZwglEltqService> {

	@Autowired
	private IZwglEltqService zwglEltqService;

	@Autowired
	private IZwglEltqszService zwglEltqszService;

	@Autowired
	private IZwglHjyzszService zwglHjyzszService;


	/*---------------------------------主表处理-begin-------------------------------------*/

	/**
	 * 分页列表查询
	 * @param zwglEltq
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "恶劣天气-分页列表查询")
	@ApiOperation(value="恶劣天气-分页列表查询", notes="恶劣天气-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<ZwglEltq>> queryPageList(ZwglEltq zwglEltq,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ZwglEltq> queryWrapper = QueryGenerator.initQueryWrapper(zwglEltq, req.getParameterMap());
		Page<ZwglEltq> page = new Page<ZwglEltq>(pageNo, pageSize);
		IPage<ZwglEltq> pageList = zwglEltqService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
     *   添加
     * @param zwglEltq
     * @return
     */
    @AutoLog(value = "恶劣天气-添加")
    @ApiOperation(value="恶劣天气-添加", notes="恶劣天气-添加")
//    @RequiresPermissions("hjyzbj:zwgl_eltq:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody ZwglEltq zwglEltq) {
        zwglEltqService.save(zwglEltq);
        return Result.OK("添加成功！");
    }

    /**
     *  编辑
     * @param zwglEltq
     * @return
     */
    @AutoLog(value = "恶劣天气-编辑")
    @ApiOperation(value="恶劣天气-编辑", notes="恶劣天气-编辑")
//    @RequiresPermissions("hjyzbj:zwgl_eltq:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
    public Result<String> edit(@RequestBody ZwglEltq zwglEltq) {
        zwglEltqService.updateById(zwglEltq);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @AutoLog(value = "恶劣天气-通过id删除")
    @ApiOperation(value="恶劣天气-通过id删除", notes="恶劣天气-通过id删除")
//    @RequiresPermissions("hjyzbj:zwgl_eltq:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name="id",required=true) String id) {
        zwglEltqService.delMain(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @AutoLog(value = "恶劣天气-批量删除")
    @ApiOperation(value="恶劣天气-批量删除", notes="恶劣天气-批量删除")
//    @RequiresPermissions("hjyzbj:zwgl_eltq:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.zwglEltqService.delBatchMain(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 导出
     * @return
     */
    @RequiresPermissions("hjyzbj:zwgl_eltq:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ZwglEltq zwglEltq) {
        return super.exportXls(request, zwglEltq, ZwglEltq.class, "恶劣天气");
    }

    /**
     * 导入
     * @return
     */
    @RequiresPermissions("hjyzbj:zwgl_eltq:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ZwglEltq.class);
    }
	/*---------------------------------主表处理-end-------------------------------------*/
	

    /*--------------------------------子表处理-恶劣天气设置-begin----------------------------------------------*/
	/**
	 * 通过主表ID查询
	 * @return
	 */
	//@AutoLog(value = "恶劣天气设置-通过主表ID查询")
	@ApiOperation(value="恶劣天气设置-通过主表ID查询", notes="恶劣天气设置-通过主表ID查询")
	@GetMapping(value = "/listZwglEltqszByMainId")
    public Result<IPage<ZwglEltqsz>> listZwglEltqszByMainId(ZwglEltqsz zwglEltqsz,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<ZwglEltqsz> queryWrapper = QueryGenerator.initQueryWrapper(zwglEltqsz, req.getParameterMap());
        Page<ZwglEltqsz> page = new Page<ZwglEltqsz>(pageNo, pageSize);
        IPage<ZwglEltqsz> pageList = zwglEltqszService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

	/**
	 * 添加
	 * @param zwglEltqsz
	 * @return
	 */
	@AutoLog(value = "恶劣天气设置-添加")
	@ApiOperation(value="恶劣天气设置-添加", notes="恶劣天气设置-添加")
	@PostMapping(value = "/addZwglEltqsz")
	public Result<String> addZwglEltqsz(@RequestBody ZwglEltqsz zwglEltqsz) {
		zwglEltqszService.save(zwglEltqsz);
		return Result.OK("添加成功！");
	}

    /**
	 * 编辑
	 * @param zwglEltqsz
	 * @return
	 */
	@AutoLog(value = "恶劣天气设置-编辑")
	@ApiOperation(value="恶劣天气设置-编辑", notes="恶劣天气设置-编辑")
	@RequestMapping(value = "/editZwglEltqsz", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> editZwglEltqsz(@RequestBody ZwglEltqsz zwglEltqsz) {
		zwglEltqszService.updateById(zwglEltqsz);
		return Result.OK("编辑成功!");
	}

	/**
	 * 通过id删除
	 * @param id
	 * @return
	 */
	@AutoLog(value = "恶劣天气设置-通过id删除")
	@ApiOperation(value="恶劣天气设置-通过id删除", notes="恶劣天气设置-通过id删除")
	@DeleteMapping(value = "/deleteZwglEltqsz")
	public Result<String> deleteZwglEltqsz(@RequestParam(name="id",required=true) String id) {
		zwglEltqszService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "恶劣天气设置-批量删除")
	@ApiOperation(value="恶劣天气设置-批量删除", notes="恶劣天气设置-批量删除")
	@DeleteMapping(value = "/deleteBatchZwglEltqsz")
	public Result<String> deleteBatchZwglEltqsz(@RequestParam(name="ids",required=true) String ids) {
	    this.zwglEltqszService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

    /**
     * 导出
     * @return
     */
    @RequestMapping(value = "/exportZwglEltqsz")
    public ModelAndView exportZwglEltqsz(HttpServletRequest request, ZwglEltqsz zwglEltqsz) {
		 // Step.1 组装查询条件
		 QueryWrapper<ZwglEltqsz> queryWrapper = QueryGenerator.initQueryWrapper(zwglEltqsz, request.getParameterMap());
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		 // Step.2 获取导出数据
		 List<ZwglEltqsz> pageList = zwglEltqszService.list(queryWrapper);
		 List<ZwglEltqsz> exportList = null;

		 // 过滤选中数据
		 String selections = request.getParameter("selections");
		 if (oConvertUtils.isNotEmpty(selections)) {
			 List<String> selectionList = Arrays.asList(selections.split(","));
			 exportList = pageList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
		 } else {
			 exportList = pageList;
		 }

		 // Step.3 AutoPoi 导出Excel
		 ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		 //此处设置的filename无效,前端会重更新设置一下
		 mv.addObject(NormalExcelConstants.FILE_NAME, "恶劣天气设置");
		 mv.addObject(NormalExcelConstants.CLASS, ZwglEltqsz.class);
		 mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("恶劣天气设置报表", "导出人:" + sysUser.getRealname(), "恶劣天气设置"));
		 mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
		 return mv;
    }

    /**
     * 导入
     * @return
     */
    @RequestMapping(value = "/importZwglEltqsz/{mainId}")
    public Result<?> importZwglEltqsz(HttpServletRequest request, HttpServletResponse response, @PathVariable("mainId") String mainId) {
		 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		 Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		 for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
       // 获取上传文件对象
			 MultipartFile file = entity.getValue();
			 ImportParams params = new ImportParams();
			 params.setTitleRows(2);
			 params.setHeadRows(1);
			 params.setNeedSave(true);
			 try {
				 List<ZwglEltqsz> list = ExcelImportUtil.importExcel(file.getInputStream(), ZwglEltqsz.class, params);
				 for (ZwglEltqsz temp : list) {
                    temp.setWj(mainId);
				 }
				 long start = System.currentTimeMillis();
				 zwglEltqszService.saveBatch(list);
				 log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
				 return Result.OK("文件导入成功！数据行数：" + list.size());
			 } catch (Exception e) {
				 log.error(e.getMessage(), e);
				 return Result.error("文件导入失败:" + e.getMessage());
			 } finally {
				 try {
					 file.getInputStream().close();
				 } catch (IOException e) {
					 e.printStackTrace();
				 }
			 }
		 }
		 return Result.error("文件导入失败！");
    }

    /*--------------------------------子表处理-恶劣天气设置-end----------------------------------------------*/

    /*--------------------------------子表处理-环境阈值设置-begin----------------------------------------------*/
	/**
	 * 通过主表ID查询
	 * @return
	 */
	//@AutoLog(value = "环境阈值设置-通过主表ID查询")
	@ApiOperation(value="环境阈值设置-通过主表ID查询", notes="环境阈值设置-通过主表ID查询")
	@GetMapping(value = "/listZwglHjyzszByMainId")
    public Result<IPage<ZwglHjyzsz>> listZwglHjyzszByMainId(ZwglHjyzsz zwglHjyzsz,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<ZwglHjyzsz> queryWrapper = QueryGenerator.initQueryWrapper(zwglHjyzsz, req.getParameterMap());
        Page<ZwglHjyzsz> page = new Page<ZwglHjyzsz>(pageNo, pageSize);
        IPage<ZwglHjyzsz> pageList = zwglHjyzszService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

	/**
	 * 添加
	 * @param zwglHjyzsz
	 * @return
	 */
	@AutoLog(value = "环境阈值设置-添加")
	@ApiOperation(value="环境阈值设置-添加", notes="环境阈值设置-添加")
	@PostMapping(value = "/addZwglHjyzsz")
	public Result<String> addZwglHjyzsz(@RequestBody ZwglHjyzsz zwglHjyzsz) {
		zwglHjyzszService.save(zwglHjyzsz);
		return Result.OK("添加成功！");
	}

    /**
	 * 编辑
	 * @param zwglHjyzsz
	 * @return
	 */
	@AutoLog(value = "环境阈值设置-编辑")
	@ApiOperation(value="环境阈值设置-编辑", notes="环境阈值设置-编辑")
	@RequestMapping(value = "/editZwglHjyzsz", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> editZwglHjyzsz(@RequestBody ZwglHjyzsz zwglHjyzsz) {
		zwglHjyzszService.updateById(zwglHjyzsz);
		return Result.OK("编辑成功!");
	}

	/**
	 * 通过id删除
	 * @param id
	 * @return
	 */
	@AutoLog(value = "环境阈值设置-通过id删除")
	@ApiOperation(value="环境阈值设置-通过id删除", notes="环境阈值设置-通过id删除")
	@DeleteMapping(value = "/deleteZwglHjyzsz")
	public Result<String> deleteZwglHjyzsz(@RequestParam(name="id",required=true) String id) {
		zwglHjyzszService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "环境阈值设置-批量删除")
	@ApiOperation(value="环境阈值设置-批量删除", notes="环境阈值设置-批量删除")
	@DeleteMapping(value = "/deleteBatchZwglHjyzsz")
	public Result<String> deleteBatchZwglHjyzsz(@RequestParam(name="ids",required=true) String ids) {
	    this.zwglHjyzszService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

    /**
     * 导出
     * @return
     */
    @RequestMapping(value = "/exportZwglHjyzsz")
    public ModelAndView exportZwglHjyzsz(HttpServletRequest request, ZwglHjyzsz zwglHjyzsz) {
		 // Step.1 组装查询条件
		 QueryWrapper<ZwglHjyzsz> queryWrapper = QueryGenerator.initQueryWrapper(zwglHjyzsz, request.getParameterMap());
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		 // Step.2 获取导出数据
		 List<ZwglHjyzsz> pageList = zwglHjyzszService.list(queryWrapper);
		 List<ZwglHjyzsz> exportList = null;

		 // 过滤选中数据
		 String selections = request.getParameter("selections");
		 if (oConvertUtils.isNotEmpty(selections)) {
			 List<String> selectionList = Arrays.asList(selections.split(","));
			 exportList = pageList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
		 } else {
			 exportList = pageList;
		 }

		 // Step.3 AutoPoi 导出Excel
		 ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		 //此处设置的filename无效,前端会重更新设置一下
		 mv.addObject(NormalExcelConstants.FILE_NAME, "环境阈值设置");
		 mv.addObject(NormalExcelConstants.CLASS, ZwglHjyzsz.class);
		 mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("环境阈值设置报表", "导出人:" + sysUser.getRealname(), "环境阈值设置"));
		 mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
		 return mv;
    }

    /**
     * 导入
     * @return
     */
    @RequestMapping(value = "/importZwglHjyzsz/{mainId}")
    public Result<?> importZwglHjyzsz(HttpServletRequest request, HttpServletResponse response, @PathVariable("mainId") String mainId) {
		 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		 Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		 for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
       // 获取上传文件对象
			 MultipartFile file = entity.getValue();
			 ImportParams params = new ImportParams();
			 params.setTitleRows(2);
			 params.setHeadRows(1);
			 params.setNeedSave(true);
			 try {
				 List<ZwglHjyzsz> list = ExcelImportUtil.importExcel(file.getInputStream(), ZwglHjyzsz.class, params);
				 for (ZwglHjyzsz temp : list) {
                    temp.setWj(mainId);
				 }
				 long start = System.currentTimeMillis();
				 zwglHjyzszService.saveBatch(list);
				 log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
				 return Result.OK("文件导入成功！数据行数：" + list.size());
			 } catch (Exception e) {
				 log.error(e.getMessage(), e);
				 return Result.error("文件导入失败:" + e.getMessage());
			 } finally {
				 try {
					 file.getInputStream().close();
				 } catch (IOException e) {
					 e.printStackTrace();
				 }
			 }
		 }
		 return Result.error("文件导入失败！");
    }

    /*--------------------------------子表处理-环境阈值设置-end----------------------------------------------*/




}
